package xuegao.study.mall.admin.service.pms.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import xuegao.study.mall.admin.dto.PmsProductCategoryParam;
import xuegao.study.mall.admin.mapper.PmsProductCategoryMapper;
import xuegao.study.mall.admin.service.pms.PmsProductCategoryService;
import xuegao.study.mall.repo.dao.pms.PmsProductCategoryAttributeRelationDao;
import xuegao.study.mall.repo.dao.pms.PmsProductCategoryDao;
import xuegao.study.mall.common.model.pms.PmsProductCategory;
import xuegao.study.mall.common.model.pms.PmsProductCategoryAttributeRelation;
import xuegao.study.mall.common.vo.pms.PmsProductCategoryVo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PmsProductCategoryServiceImpl implements PmsProductCategoryService {

    @Autowired
    private PmsProductCategoryDao pmsProductCategoryDao;

    @Autowired
    private PmsProductCategoryAttributeRelationDao pmsProductCategoryAttributeRelationDao;

    @Autowired
    private PmsProductCategoryMapper pmsProductCategoryMapper;

    @Override
    public List<PmsProductCategory> list(Long parentId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String, Object> params = new HashMap<>();
        params.put("parent_id", parentId);
        return pmsProductCategoryDao.selectByMap(params);
    }

    @Override
    public List<PmsProductCategoryVo> listWithChildren() {
        List<PmsProductCategoryVo> voResults = new ArrayList<>();
        Map<String, Object> params = new HashMap<>();
        List<PmsProductCategory> allResultList = pmsProductCategoryDao.selectByMap(params);
        if (allResultList == null || allResultList.size() == 0) {
            return voResults;
        }
        Map<Integer, List<PmsProductCategory>> levelMap = allResultList.stream().collect(Collectors.groupingBy(PmsProductCategory::getLevel));
        List<PmsProductCategory> topLevelList = levelMap.get(0);
        List<PmsProductCategory> secondLevelList = levelMap.get(1);
        Map<Long, List<PmsProductCategory>> parentIdMap = new HashMap<>();
        if (secondLevelList != null && secondLevelList.size() > 0) {
            parentIdMap = secondLevelList.stream().collect(Collectors.groupingBy(PmsProductCategory::getParentId));
        }
        for(PmsProductCategory category : topLevelList) {
            PmsProductCategoryVo vo = pmsProductCategoryMapper.pmsProductCategory2Vo(category);
            List<PmsProductCategory> itemList = parentIdMap.get(vo.getId());
            List<PmsProductCategoryVo> childrenList = new ArrayList<>();
            if (itemList != null) {
                for(PmsProductCategory item : itemList) {
                    PmsProductCategoryVo itemVo = pmsProductCategoryMapper.pmsProductCategory2Vo(item);
                    childrenList.add(itemVo);
                }
                vo.setChildren(childrenList);
            }
            voResults.add(vo);
        }
        return voResults;
    }

    @Override
    public PmsProductCategory getItem(Long id) {
        return pmsProductCategoryDao.selectById(id);
    }

    @Override
    public int create(PmsProductCategoryParam param) {
        PmsProductCategory category = new PmsProductCategory();
        category.setProductCount(0);
        BeanUtils.copyProperties(param, category);
        // 没有父分类时为一级分类
        setCategoryLevel(category);
        int count = pmsProductCategoryDao.insert(category);
        // 创建筛选属性关联
        List<Long> productAttributeIdList = param.getProductAttributeIdList();
        insertRelations(category.getId(), productAttributeIdList);
        return count;
    }

    @Override
    public int update(Long id, PmsProductCategoryParam param) {
        PmsProductCategory category = new PmsProductCategory();
        category.setId(id);
        BeanUtils.copyProperties(param, category);
        setCategoryLevel(category);
        // todo 更新商品分类时更新商品中的名称

        List<Long> productAttributeIdList = param.getProductAttributeIdList();
        Map<String, Object> delParams = new HashMap<>();
        delParams.put("product_category_id", id);
        pmsProductCategoryAttributeRelationDao.deleteByMap(delParams);
        insertRelations(id, productAttributeIdList);
        return pmsProductCategoryDao.updateById(category);
    }

    private void setCategoryLevel(PmsProductCategory category) {
        if (category.getParentId() == 0) {
            category.setLevel(0);
        } else {
            PmsProductCategory parentCategory = pmsProductCategoryDao.selectById(category.getParentId());
            if (parentCategory != null) {
                category.setLevel(parentCategory.getLevel() + 1);
            } else {
                category.setLevel(0);
            }
        }
    }
    private void insertRelations(Long categoryId, List<Long> productAttributeIdList) {
        if (!CollectionUtils.isEmpty(productAttributeIdList)) {
            for (Long attributeId : productAttributeIdList) {
                PmsProductCategoryAttributeRelation relation = new PmsProductCategoryAttributeRelation();
                relation.setProductAttributeId(attributeId);
                relation.setProductCategoryId(categoryId);
                pmsProductCategoryAttributeRelationDao.insert(relation);
            }
        }
    }
}
